package com.xiaoyu.lanling.feature.accost.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.accost.QuickAccostUserClickEvent
import com.xiaoyu.lanling.feature.accost.model.QuickAccostUserItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import com.xiaoyu.lanling.view.text.UserNameTextView

class QuickAccostUserViewHolder : ViewHolderBase<QuickAccostUserItem>() {

    companion object {
        private val sOnItemClickListener = View.OnClickListener {
            val itemData = it.getViewTag<QuickAccostUserItem>() ?: return@OnClickListener
            itemData.toggle()

            it.isSelected = itemData.selected
            QuickAccostUserClickEvent().post()
        }
    }

    private var mAvatar: UserAvatarDraweeView? = null
    private var mName: UserNameTextView? = null
    private var mCheckView: ImageView? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.quick_accost_user_item, parent, false)
        mAvatar = view.findViewById(R.id.avatar)
        mName = view.findViewById(R.id.name)
        mCheckView = view.findViewById(R.id.check_view)
        mCheckView?.setOnClickDebounceListener(sOnItemClickListener)
        return view
    }

    override fun showData(position: Int, itemData: QuickAccostUserItem) {
        ImageViewUtil.loadImage(mAvatar, itemData.user, 80)
        mName?.setUser(itemData.user)
        mCheckView?.isSelected = itemData.selected
        mCheckView?.setViewTag(itemData)
    }
}